package cn.com.huzo.webapp.controller.admin;

import huzofw.model.entities.security.ActionEntity;
import huzofw.model.entities.security.ResourceEntity;
import huzofw.service.security.ResourceManager;
import huzofw.common.web.controller.BaseFormController;
import huzofw.model.entities.viewmodel.TreeNode;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 系统资源控制器
 *
 * @author 吴旭
 */
@Controller
public class ResourceController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "ResourceManager")
    private ResourceManager _manager;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到系统资源编辑画面
     * <p>
     * @return
     */
    @RequestMapping("admin/resource/list.html")
    public ModelAndView redirect() {
        ModelAndView view = new ModelAndView();
        view.setViewName("admin.resource.list.def");
        return view;
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 取得系统操作数据。
     *
     * @return 系统操作列表。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/resource/fetchAllAction.html",
            method = RequestMethod.POST)
    @ResponseBody
    public List<ActionEntity> getAllAction() throws Exception {
        return _manager.fetchAllAction();
    }

    /**
     * 取得系统资源树节点数据。
     *
     * @param key 系统资源标识
     * @return 部们数据对象。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/resource/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    ResourceEntity fetchByKey(@RequestBody Long key) throws Exception {
        return _manager.fetchByKey(key);
    }

    /**
     * 取得系统资源数节点数据。
     *
     * @param parentKey 所属系统资源标识
     * @return 该系统资源下的子部们数据节点列表。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/resource/tree.html",
            method = RequestMethod.GET)
    @ResponseBody
    public List<TreeNode> getResourceTreeNode(@RequestParam Long parentKey) throws Exception {
        List<TreeNode> _listResourceTree = _manager.fetchTreeNodeByParentKey(parentKey);
        return _listResourceTree;
    }

    /**
     * 保存系统资源数据。
     * <p>
     * @param instance 要保存的系统资源对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "admin/resource/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody ResourceEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getResourceId() > 0) { //编辑
            rowsAffected = (long) _manager.update(instance); // 返回影响行数
        } else {//新增
            rowsAffected = _manager.insert(instance);        // 返回主键
        }

        return rowsAffected;
    }

    /**
     * 更改所属目录。
     *
     * @param nodeParams 当前目录及父目录标识
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/resource/changeParentCatalogue.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer changeParentCatalogue(@RequestBody final Map<String, Object> nodeParams) throws Exception {
        return _manager.changeParentCatalogue(nodeParams);
    }

    /**
     * 删除系统资源数据。
     *
     * @param instance 要删除的系统资源
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "admin/resource/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer delete(@RequestBody ResourceEntity instance) throws Exception {
        return _manager.delete(instance);
    }
    // </editor-fold>
}
